<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Company extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model ( 'function_model' );
		$this->load->model ( 'MChain' );
		$this->load->library('Encrypt');
		//$this->output->enable_profiler(true);
		if(($this->session->userdata('admin_name')==""))
		{
			echo "<script>alert('Please Login again');</script>";
			redirect("login/admin_login", "refresh");
		}
	}
	
	
	public function index() {
		$data ['title'] = 'Signup Company';
		$data ['groups'] = $this->function_model->getProvince ();
		$data ['option_province'] = $this->MChain->getprovince ();
		$this->load->view ( 'header', $data );
		$this->load->view ( "company_register", $data );
	}
	
	
	public function company_list() {
		$data ['title'] = 'Company';
		$data ['groups'] = $this->function_model->getCompany ();
		$this->load->view ( 'header', $data );
		$this->load->view ( "company_list", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function manage_end_user() {
		$data ['title'] = 'Manage End User';
		$data ['groups'] = $this->function_model->manage_end_user ();
		$this->load->view ( 'header', $data );
		$this->load->view ( "manage_end_user", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function province_list() {
		$data ['title'] = 'Province';
		$data ['groups'] = $this->function_model->getProvince ();
		$this->load->view ( 'header', $data );
		$this->load->view ( "province_list", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function district_list() {
		$data ['title'] = 'District';
		$data ['groups'] = $this->function_model->getDistrict ();
		$this->load->view ( 'header', $data );
		$this->load->view ( "district_list", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function main_company(){
		$data['title']= 'Signup Company';
		$data['groups'] = $this->function_model->getProvince();
		$data['option_province'] = $this->MChain->getprovince();
		$this->load->view('header',$data);
		$this->load->view("company_register", $data);
		$this->load->view('footer',$data);
	}
	
	
	public function welcome() {
		$data ['title'] = 'Welcome';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'welcome', $data );
		$this->load->view ( 'footer', $data );
	}

	
	public function thank_add() {
		$data ['title'] = 'Thank';
		$this->load->view ( 'header', $data );
		$this->load->view ( 'company_success', $data );
		$this->load->view('footer',$data);
	}
	

	public function add_company() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
		$this->form_validation->set_rules ( 'email', 'Your Email', 'trim|required|valid_email|unique[tbl_company.email]|min_length[5]|max_length[100]' );
		$this->form_validation->set_rules ( 'tel_number1', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number2', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number3', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'address', 'Address', 'trim|required|xss_clean' );
		$this->form_validation->set_rules ( 'fax1', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax2', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax3', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'account_user', 'Account User', 'trim|required|min_length[4]|xss_clean|unique[tbl_admin.account_user]|valid_eng_num|valid_space' );
		$this->form_validation->set_rules ( 'password', 'Password', 'trim|required|min_length[4]|max_length[32]|valid_eng_num|valid_space' );
		$this->form_validation->set_rules ( 'password1', 'Password Confirmation', 'trim|required|matches[password]|valid_eng_num|valid_space' );
	//	$this->form_validation->set_rules ( 'province', 'Province', 'trim|required' );
	//	$this->form_validation->set_rules ( 'district', 'District', 'trim|required' );
		$this->form_validation->set_rules ( 'con_tel1', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_tel2', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_tel3', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_name', 'Contact Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->main_company ();
		} else {
			$this->function_model->add_company ();
			// $id = $this->db->insert_id();
			// $member = $this->function_model->get_member_by_id($id);
			// $this->send_email("activate", $member);
			$this->thank_add ();
		}
	}
	
	
	public function company_edit($id) {
		$data ['title'] = 'Company';
		$data ['groups'] = $this->function_model->getCompanyInfo ($id);
		$data ['branch'] = $this->function_model->getBranchCompany ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "company_edit", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function edit_company() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
		$this->form_validation->set_rules ( 'email', 'Your Email', 'trim|required|valid_email|min_length[5]|max_length[100]' );
		$this->form_validation->set_rules ( 'tel_number1', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number2', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'tel_number3', 'Telephone Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'address', 'Address', 'trim|required|xss_clean' );
		$this->form_validation->set_rules ( 'fax1', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax2', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'fax3', 'Fax Number', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'account_user', 'Account User', 'trim|required|min_length[4]|xss_clean|valid_eng_num|valid_space' );
		//$this->form_validation->set_rules ( 'password', 'Password', 'trim|required|min_length[4]|max_length[32]' );
		//$this->form_validation->set_rules ( 'password1', 'Password Confirmation', 'trim|required|matches[password]' );
		//$this->form_validation->set_rules ( 'province', 'Province', 'trim|required' );
		//$this->form_validation->set_rules ( 'district', 'District', 'trim|required' );
		$this->form_validation->set_rules ( 'con_tel1', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_tel2', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_tel3', 'Contact Telephone', 'trim|required|is_numeric|min_length[1]|max_length[4]' );
		$this->form_validation->set_rules ( 'con_name', 'Contact Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
	
		if ($this->form_validation->run () == FALSE) {
			$this->company_edit ();
		} else {
			$this->function_model->edit_company ();
			// $id = $this->db->insert_id();
			// $member = $this->function_model->get_member_by_id($id);
			// $this->send_email("activate", $member);
			$this->thank_add ();
		}
	}
	
	
	public function province_add() {
		$data ['title'] = 'Add Province';
		$this->load->view ( 'header', $data );
		$this->load->view ( "province_add", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function province_action() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|unique[tbl_province.PROVINCE_NAME]|valid_string' );
		
		if ($this->form_validation->run () == FALSE) {
			$this->province_add();
		} else {
			$this->function_model->province_add ();
			$this->thank_add ();
		}
	}
	
	
	public function province_delete($id) {
		$this->function_model->province_delete ($id);
		$this->thank_add();
	}
	
	
	public function province_delete_all() {
		$this->function_model->province_delete_all ();
		$this->thank_add();
	}
	
	
	public function province_edit($id) {
		$data ['title'] = 'Edit Province';
		$data ['province'] = $this->function_model->select_province ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "province_edit", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function province_action_edit() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
	
		if ($this->form_validation->run () == FALSE) {
			$this->province_edit();
		} else {
			$this->function_model->province_edit ();
			$this->thank_add ();
		}
	}
	
	
	public function district_add() {
		$data ['title'] = 'Add District';
		$data['option_province'] = $this->MChain->getprovince();
		$this->load->view ( 'header', $data );
		$this->load->view ( "district_add", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function district_action() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
		$this->form_validation->set_rules ( 'province', 'Province', 'trim|required' );
	
		if ($this->form_validation->run () == FALSE) {
			$this->district_add();
		} else {
			$this->function_model->district_add ();
			$this->thank_add ();
		}
	}
	
	
	public function district_edit($id) {
		$data ['title'] = 'Edit District';
		$data ['select_district'] = $this->MChain->select_province ($id);
		$data ['district'] = $this->function_model->select_district ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "district_edit", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function district_action_edit() {
		$this->load->library ( 'form_validation' );
		// field name, error message, validation rules
		$this->form_validation->set_rules ( 'name', 'Name', 'trim|required|xss_clean|min_length[2]|max_length[100]|valid_string' );
		$this->form_validation->set_rules ( 'province', 'Province', 'trim|required' );
	
		if ($this->form_validation->run () == FALSE) {
			$this->district_edit();
		} else {
			$this->function_model->district_edit ();
			$this->thank_add ();
		}
	}
	
	
	public function district_delete($id) {
		$this->function_model->district_delete ($id);
		$this->thank_add();
	}
	
	
	public function district_delete_all() {
		$this->function_model->district_delete_all ();
		$this->thank_add();
	}
	
	
	public function end_user_edit($id) {
		$data ['title'] = 'Edit End User';
		$data ['groups'] = $this->function_model->get_end_user ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "end_user_edit", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function edit_end_user() {
		$this->function_model->end_user_edit ();
		$this->thank_add ();
	}
	
	
	public function end_user_delete($id) {
		$this->function_model->end_user_delete ($id);
		$this->thank_add ();
	}
	
	
	public function end_user_delete_all() {
		$this->function_model->end_user_delete_all ();
		$this->thank_add ();
	}
	
	
	public function end_user_preview($id) {
		$data ['title'] = 'Edit End User';
		$data ['groups'] = $this->function_model->get_end_user ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "end_user_preview", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function show_email($id) {
		$data ['title'] = 'Company';
		$data ['groups'] = $this->function_model->getCompanyInfo ($id);
		$data ['branch'] = $this->function_model->getBranchCompany ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "company_email", $data );
		$this->load->view('footer',$data);
	}
	
	
	function get_email($id) {
		$member = $this->function_model->getCompanyEmail($id);
		$this->send_email("activate", $member);
	}
	

	function send_email($type, $member) {
		$config = array (
				'protocol' => 'smtp',
				'smtp_host' => 'ssl://smtp.googlemail.com',
				'smtp_port' => 465,
				'smtp_user' => 'pemiga.me@gmail.com',
				'smtp_pass' => '14022511mana',
				'mailtype' => 'html',
				'charset' => 'utf-8' 
		);
		
		$this->load->library ( "email", $config );
		$this->email->set_newline ( "\r\n" );
		$this->email->from ( 'Team Packs Asia' );
		$this->email->to ( $member->email );
		
		if ($type == "activate") {
			$this->email->subject ( 'Register Success !!' );
			$data = array (
					"name" => $member->name,
					"email" => $member->email,
					"tel" => $member->tel_number,
					"fax" => $member->fax,
					"address" => $member->address,
					"account" => $member->account_user,
					"pass" =>  $member->password,
					"con_name" => $member->contact_name,
					"con_tel" => $member->contact_number,
					"province" => $member->PROVINCE_NAME,
					"district" => $member->district_NAME,
			);
			$data ['branch'] = $this->function_model->getBranchCompany ($member->company_id);
			$email_message = $this->load->view ( "activate-email-page", $data, true );
			$this->email->message ( $email_message );
		}
		$this->email->send ();
		$this->function_model->update_status_email ($member->company_id);
		$this->thank_add();
	}
	
	
	public function preview($id) {
		$data ['title'] = 'Company';
		$data ['groups'] = $this->function_model->getCompanyInfo ($id);
		$data ['branch'] = $this->function_model->getBranchCompany ($id);
		$this->load->view ( 'header', $data );
		$this->load->view ( "company_preview", $data );
		$this->load->view('footer',$data);
	}
	
	
	public function company_delete($id) {
		$this->function_model->company_delete ($id);
		$this->thank_add();
	}
	
	
	public function delete_all() {
		$this->function_model->company_delete_all ();
		$this->thank_add();
	}
	
	
	function selectdistrict($id = "") {
		$data ['option_district'] = $this->MChain->get_district ( $id );
		$this->load->view ( 'selectDistrict', $data );
	}
	
	
	function selectSubdistrict($id = "") {
		$data ['option_province'] = $this->MChain->getSubdistrict ( $id );
		$this->load->view ( 'selectSubdistrict', $data );
	}
	
	
	function selectSubprovince() {
		$data ['option_province'] = $this->MChain->subprovince ();
		$this->load->view ( 'selectSubprovince', $data );
	}
	
	
}
?>